﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IP_PROJECT
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DeleteArticle_Click(object sender, EventArgs e)
        {
            String id = ((Button)sender).CommandArgument;
            DataClassesDataContext db = new DataClassesDataContext();
            var result = from u in db.Articles
                         where u.Id == int.Parse(id)
                         select u;
            Article article = result.First();
            db.Articles.DeleteOnSubmit(article);
            db.SubmitChanges();
            Response.Redirect(Request.Url.ToString());
        }

        protected void SaveArticle_Click(object sender, EventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            Article article = new Article();
            TextBox title = AddNewArticleView.FindControl("ArticleTitle") as TextBox;
            TextBox description = AddNewArticleView.FindControl("ArticleDescription") as TextBox;
            TextBox content = AddNewArticleView.FindControl("ArticleContent") as TextBox;
            article.Title = title.Text;
            article.Description = description.Text;
            article.Content = content.Text;
            db.Articles.InsertOnSubmit(article);
            db.SubmitChanges();
            Response.Redirect(Request.Url.ToString());
        }
    }
}